﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Christian
 * Datum: 14.05.2010
 * Zeit: 16:30
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Globalization;

namespace MovieExplorer
{
    /// <summary>
    /// Description of MovieInformationDialog.
    /// </summary>
    public partial class MovieInformationDialog : Form
    {
        private MovieInformation _MovieInfo = null;
        private bool _Dirty = false;

        public MovieInformationDialog(MovieInformation movieInfo)
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //

            MovieInfo = movieInfo;
        }

        public bool Dirty
        {
            get { return _Dirty; }
        }

        public MovieInformation MovieInfo
        {

            get
            {
                if (_MovieInfo != null)
                {
                    _MovieInfo.title = tbTitle.Text;
                    _MovieInfo.path = tbPath.Text;
                    _MovieInfo.filename = tbFilename.Text;
                    //_MovieInfo.releasedate = tbReleaseDate.Text;
                    _MovieInfo.releasedate = dtpReleaseDate.Value.ToString("yyyy-MM-dd");
                    _MovieInfo.Backdrop = tbBackdrop.Text;
                    _MovieInfo.Folder = tbFolder.Text;
                    //_MovieInfo.Rating = tbRating.Text;
                    _MovieInfo.Rating = string.Format("{0:0.0}", (double)trbRating.Value / 10.0);
                    _MovieInfo.Runtime_Original = tbRuntime.Text;
                    _MovieInfo.People_Actors = tbActors.Text;
                    _MovieInfo.People_Director = tbDirector.Text;
                    _MovieInfo.AlternativeTitles = tbAlternativeTitles.Text;
                    _MovieInfo.Budget = tbBudget.Text;
                    _MovieInfo.ProductionCountry = tbProductionCountry.Text;
                    _MovieInfo.Description = tbDescription.Text;
                }

                return _MovieInfo;
            }
            set
            {
                _MovieInfo = value;

                tbTitle.Text = _MovieInfo.title;
                tbPath.Text = _MovieInfo.path;
                tbFilename.Text = _MovieInfo.filename;

                if (!Directory.Exists(tbPath.Text))
                {
                    tbPath.BackColor = Color.Red;
                }
                else
                {
                    tbPath.BackColor = Color.Lime;
                }
                if (!File.Exists(tbFilename.Text))
                {
                    tbFilename.BackColor = Color.Red;
                }
                else
                {
                    tbFilename.BackColor = Color.Lime;
                }

                //tbReleaseDate.Text = _MovieInfo.releasedate;
                try
                {
                    dtpReleaseDate.Value = DateTime.ParseExact(_MovieInfo.releasedate, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                }
                catch
                {
                    dtpReleaseDate.Value = DateTime.Now;
                }

                tbBackdrop.Text = _MovieInfo.Backdrop;
                UpdateBackdrop();

                tbFolder.Text = _MovieInfo.Folder;
                UpdateFolder();

                //tbRating.Text = _MovieInfo.Rating;
                try
                {
                    double dblval = double.Parse(_MovieInfo.Rating.Replace(".", ","));
                    trbRating.Value = (int)Math.Round(dblval * 10.0);
                }
                catch
                {
                    trbRating.Value = 0;
                }
                tbRuntime.Text = _MovieInfo.Runtime_Original;
                tbActors.Text = _MovieInfo.People_Actors;
                tbDirector.Text = _MovieInfo.People_Director;
                tbAlternativeTitles.Text = _MovieInfo.AlternativeTitles;
                tbBudget.Text = _MovieInfo.Budget;
                tbProductionCountry.Text = _MovieInfo.ProductionCountry;
                tbDescription.Text = _MovieInfo.Description;
            }
        }

        private void UpdateFolder()
        {
            if (File.Exists(tbFolder.Text))
            {
                pictureBoxFolder.Load(tbFolder.Text);
            }
            else
            {
                pictureBoxFolder.Image = null;
            }
        }

        private void ShowBackdrop(int value)
        {
            string[] backdrops = tbBackdrop.Text.Split(new string[] { "\"", "{", "}", "," }, StringSplitOptions.RemoveEmptyEntries);
            if (backdrops.Length >= value && value >= 1)
            {
                if (File.Exists(backdrops[value - 1]))
                {
                    pictureBoxBackdrop.Load(backdrops[value - 1]);
                }
                else
                {
                    pictureBoxBackdrop = null;
                }
            }
            else
            {
                pictureBoxBackdrop = null;
            }
        }

        private void UpdateBackdrop()
        {
            string[] backdrops = tbBackdrop.Text.Split(new string[] { "\"", "{", "}", "," }, StringSplitOptions.RemoveEmptyEntries);
            if (backdrops.Length > 1)
            {
                trackBarBackdrops.Visible = true;
            }
            else
            {
                trackBarBackdrops.Visible = false;
            }

            trackBarBackdrops.Minimum = 1;
            trackBarBackdrops.Maximum = backdrops.Length;
            if(trackBarBackdrops.Maximum > 0)
                trackBarBackdrops.Value = 1;

            ShowBackdrop(1);
        }

        private void AddBackdrop(string filename)
        {
            string newFilename = Path.Combine(Path.Combine(_MovieInfo.path, "Backdrops"), Path.GetFileName(filename));
            File.Copy(openFileDialog.FileName, newFilename, true);

            if (tbBackdrop.Text.Length > 0)
            {
                tbBackdrop.Text += ",";
            }
            tbBackdrop.Text += "\"" + newFilename + "\"";

            UpdateBackdrop();

            _Dirty = true;
        }

        private void RemoveBackdrop(int value)
        {
            string[] backdrops = tbBackdrop.Text.Split(new string[] { "\"", "{", "}", "," }, StringSplitOptions.RemoveEmptyEntries);
            if (backdrops.Length >= value && value >= 1)
            {
                if (File.Exists(backdrops[value - 1]))
                {
                    File.Delete(backdrops[value - 1]);
                    backdrops[value - 1] = "";

                    string newBackdropString = "";
                    foreach (string backdrop in backdrops)
                    {
                        if (!backdrop.Equals(""))
                        {
                            if (!newBackdropString.Equals(""))
                            {
                                newBackdropString += ",";
                            }
                            newBackdropString += "\"" + backdrop + "\"";
                        }
                    }

                    tbBackdrop.Text = newBackdropString;
                    
                    UpdateBackdrop();

                    _Dirty = true;
                }
            }
        }

        void BtnQPathClick(object sender, EventArgs e)
        {
            if (!Directory.Exists(tbPath.Text))
            {
                MessageBox.Show("Directory \"" + tbPath.Text + "\" does not exist!");
            }
            else
            {
                MessageBox.Show("Everything OK!");
            }
        }

        void BtnQFileClick(object sender, EventArgs e)
        {
            if (!File.Exists(tbFilename.Text))
            {
                MessageBox.Show("File \"" + tbFilename.Text + "\" does not exist!");
            }
            else
            {
                MessageBox.Show("Everything OK!");
            }
        }

        void BtnLoadCoverClick(object sender, EventArgs e)
        {
            if (File.Exists(tbFolder.Text))
            {
                if (MessageBox.Show("Soll das vorhandene Cover \"" + tbFolder.Text + "\" durch ein anderes ersetzt werden?", "Cover ersetzen", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
            }

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string newFilename = Path.Combine(_MovieInfo.path, Path.GetFileName(openFileDialog.FileName));
                File.Copy(openFileDialog.FileName, newFilename, true);
                
                tbFolder.Text = newFilename;
                UpdateFolder();
                _Dirty = true;
            }
        }

        void BtnLoadBackdropClick(object sender, EventArgs e)
        {
            openFileDialog.FileName = tbFolder.Text;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                AddBackdrop(openFileDialog.FileName);
            }
        }

        private void trackBarBackdrops_ValueChanged(object sender, EventArgs e)
        {
            ShowBackdrop(trackBarBackdrops.Value);
        }

        private void btnRemoveFolder_Click(object sender, EventArgs e)
        {
            if (File.Exists(tbFolder.Text))
            {
                File.Delete(tbFolder.Text);
                tbFolder.Text = "";

                UpdateFolder();
                _Dirty = true;
            }
        }

        private void btnRemoveBackdrop_Click(object sender, EventArgs e)
        {
            RemoveBackdrop(trackBarBackdrops.Value);
        }

        private void trbRating_ValueChanged(object sender, EventArgs e)
        {
            lblRating.Text = string.Format("{0:0.0}", (double)trbRating.Value / 10.0);
        }
    }
}
